package com.leyuangame.core.config;

import com.leyuangame.core.model.RebotConfig;
import org.springframework.beans.BeanUtils;

import java.io.*;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Properties;

public class SysConfig {
    private static final String rebotconfig = "robot.properties";

    private static final String rebotisopen = "rebotisopen";
    private static final String rebotmingold = "rebotmingold";
    private static final String rebotmaxgold = "rebotmaxgold";
    public static Properties rebotconf = new Properties();
    public static RebotConfig rebotConfig = new RebotConfig();
    private static InputStream in = null;
    private static InputStream out = null;


    //private static final OutputStream rebotout = ;
    static {
        try {
            final InputStream rebotin = SysConfig.class.getClassLoader().getResourceAsStream(rebotconfig);
            rebotconf.load(rebotin) ;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void saveRebot(RebotConfig rebotConfig){
        try {
            rebotconf.put(rebotisopen,rebotConfig.getIsopen()+"");
            rebotconf.put(rebotmingold,rebotConfig.getMingold().toString());
            rebotconf.put(rebotmaxgold,rebotConfig.getMaxgold().toString());
            FileOutputStream out = new FileOutputStream(SysConfig.class.getClassLoader().getResource(rebotconfig).getFile());
            rebotconf.store(out,new Date().toString());
            loadRebot();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void loadRebot(){
        try {
            final InputStream rebotin = SysConfig.class.getClassLoader().getResourceAsStream(rebotconfig);
            rebotconf.load(rebotin) ;
            rebotConfig.setIsopen(Integer.parseInt(rebotconf.getProperty(rebotisopen)));
            rebotConfig.setMaxgold(new BigDecimal(rebotconf.getProperty(rebotmaxgold)));
            rebotConfig.setMingold(new BigDecimal(rebotconf.getProperty(rebotmingold)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
